package flc.ast.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.MyDebugBean;
import flc.ast.databinding.ActivityAddDebugBinding;
import flc.ast.utils.AppSpUtils;

public class AddDebugActivity extends BaseAc<ActivityAddDebugBinding> {

    public static int type;

    @Override
    protected int onCreate() {
        return R.layout.activity_add_debug;
    }

    @Override
    protected void initView() {
        mDataBinding.ivAddDebugBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvSave.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        if (v.getId() == R.id.tvSave) {//保存
            String name = mDataBinding.etName.getText().toString().trim();
            String ip = mDataBinding.tvIp.getText().toString().trim();
            String host = mDataBinding.etHost.getText().toString().trim();
            String port = mDataBinding.etPort.getText().toString().trim();
            if (TextUtils.isEmpty(name)) {
                ToastUtils.showShort(R.string.please_input_name);
                return;
            }
            if (type == 1 || type == 3) {
                if (TextUtils.isEmpty(host)) {
                    ToastUtils.showShort(R.string.please_input_host);
                    return;
                }
            }
            if (TextUtils.isEmpty(port)) {
                ToastUtils.showShort(R.string.please_input_port);
                return;
            }
            if (type == 0) {
                List<MyDebugBean> listSp = AppSpUtils.getTcpServerList();
                String type = getString(R.string.tcp_server);
                if (listSp == null) {
                    listSp = new ArrayList<>();
                }
                listSp.add(new MyDebugBean(name, ip, port, type));
                AppSpUtils.saveTcpServerList(listSp);
            } else if (type == 1) {
                List<MyDebugBean> listSp = AppSpUtils.getTcpClientList();
                String type = getString(R.string.tcp_client);
                if (listSp == null) {
                    listSp = new ArrayList<>();
                }
                listSp.add(new MyDebugBean(name, ip + host, port, type));
                AppSpUtils.saveTcpClientList(listSp);
            } else if (type == 2) {
                List<MyDebugBean> listSp = AppSpUtils.getUdpServerList();
                String type = getString(R.string.udp_server);
                if (listSp == null) {
                    listSp = new ArrayList<>();
                }
                listSp.add(new MyDebugBean(name, ip, port, type));
                AppSpUtils.saveUdpServerList(listSp);
            } else if (type == 3) {
                List<MyDebugBean> listSp = AppSpUtils.getUdpClientList();
                String type = getString(R.string.udp_client);
                if (listSp == null) {
                    listSp = new ArrayList<>();
                }
                listSp.add(new MyDebugBean(name, ip + host, port, type));
                AppSpUtils.saveUdpClientList(listSp);
            }
            ToastUtils.showShort(getString(R.string.save_success));
            finish();
        }
    }

    @Override
    protected void initData() {
        String ip = getIp();
        if (type == 0 || type == 2) {
            mDataBinding.tvIp.setText(ip);
            mDataBinding.etHost.setVisibility(View.GONE);
        } else {
            mDataBinding.tvIp.setText(ip.substring(0, ip.lastIndexOf(".") + 1));
            mDataBinding.etHost.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 返回当前设备的ip地址
     *
     * @return
     */
    private String getIp() {
        String ip = NetworkUtils.getIPAddress(true);
        ;
        try {
            String wifiIp = NetworkUtils.getIpAddressByWifi();
            //这里添加这样的处理是因为发现有在小米的Android12系统的手机上通过NetworkUtils.getIPAddress无法
            //获取到局域网地址，而是直接获取到公网地址
            if (!TextUtils.isEmpty(wifiIp) && wifiIp.startsWith("192")) {
                return wifiIp;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ip;
    }

}